function MyAxios(options) {
  return new Promise((reslove, rejecet) => {
    if (options.params) {
      const params = new URLSearchParams(options.params)
      options.url += `?${params.toString()}`
    
    }

    const xhr = new XMLHttpRequest()
    xhr.open(options.method|| 'GET', options.url)
    xhr.addEventListener('loadend', function () {
      if (xhr.status >= 200 && xhr.status < 300) {
        reslove(JSON.parse(xhr.response))
      } else {
        rejecet(new Error(xhr.response))
      }
    })
    if(options.data){
      xhr.setRequestHeader('Content-Type','application/json')
      xhr.send(JSON.stringify(options.data))
    }else{
      xhr.send()
    }
   
  })
}

MyAxios({
  url: 'http://hmajax.itheima.net/api/register',
  method:'POST',
  data: {
    username:'admin',
    password:'123456'
  },
}).then((res) => {
  console.log(res)
})
